package com.example.badgebutton_ohos.badg;




import com.example.badgebutton_ohos.BadgeButton;
import com.example.badgebutton_ohos.ResourceTable;
import ohos.aafwk.ability.AbilityForm;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.ability.OnClickListener;
import ohos.aafwk.ability.ViewsStatus;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.TabList;
import ohos.agp.components.TableLayout;
import ohos.agp.components.element.Element;
import ohos.agp.components.element.PixelMapElement;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.components.element.StateElement;
import ohos.global.resource.NotExistException;
import ohos.global.resource.Resource;

import java.io.IOException;


public class MainActivity extends AbilitySlice implements Component.ClickedListener {

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        TabList tabs = (TabList) findComponentById(ResourceTable.id.tabs);
        addTabItem(tabs, "首页", ResourceTable.mipmap.list_1, false, "");
        addTabItem(tabs, "分类", ResourceTable.mipmap.list_2, false, "");
        addTabItem(tabs, "消息", ResourceTable.mipmap.list_3, true, "9");
        addTabItem(tabs, "发现", ResourceTable.mipmap.list_4, false, "");
        addTabItem(tabs, "我的", ResourceTable.mipmap.list_5, true, "");
        tabs.getTabAt(0).select();
     }

    void addTabItem(TabList tabs, String text, int icon, boolean visible, String badge) {
        BadgeButton item = (BadgeButton) getLayoutInflater().inflate(R.layout.item_tab, tabs, false);
        item.setIcon(wrap(icon));
        item.setText(text);
        item.setBadgeText(badge);
        item.setBadgeVisible(visible);
        item.setLayoutConfig(new ComponentContainer.LayoutConfig(ComponentContainer.LayoutConfig.MATCH_PARENT, ComponentContainer.LayoutConfig.MATCH_CONTENT));
        tabs.addTab(tabs.newTab().setCustomView(item));
    }

    ColorStateList mTint;

    Element wrap(int icon) {
       // Element drawable = ResourcesCompat.getDrawable(getResources(),icon, getTheme());
        PixelMapElement drawable = null;
        try {
            Resource re = getResourceManager().getResource(icon);
            drawable=new PixelMapElement(re);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (NotExistException e) {
            e.printStackTrace();
        }
        if (mTint == null) {
           // mTint = ResourcesCompat.getColorStateList(getResources(), R.color.tab, getTheme());
            mTint=new StateElement(getContext(),R.color.tab);
        }
        if (drawable != null) {
            drawable = DrawableCompat.wrap(drawable.mutate());
            DrawableCompat.setTintList(drawable, mTint);
        }
        return drawable;
    }



    @Override
    public void onClick(Component component) {

    }
}
